home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / Kant Pro source Folder / Kant Pro 1.1 ƒ / Shell ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-19  |  10.3 KB  |  387 lines  |  [TEXT/MMCC]

  1. #define USE_MERCUTIO        0
  2.  
  3. #include "main.h"
  4. #include "drag utilities.h"
  5. #include "apple events.h"
  6. #include "integrity.h"
  7. #include "menus.h"
  8. #include "prefs.h"
  9. #include "environment.h"
  10. #include "error.h"
  11. #include "print meat.h"
  12. #include "graphics.h"
  13. #include "graphics dispatch.h"
  14. #include "window layer.h"
  15. #include "program globals.h"
  16. #include "kant.h"
  17. #include <AppleEvents.h>
  18. #include <EPPC.h>
  19. #if USE_MERCUTIO
  20. #include "Mercutio API.h"
  21. #endif
  22.  
  23. static    short            gTheCurrentModifiers;
  24.  
  25. void main(void)
  26. {
  27.     Boolean            programIntegrityVerified;
  28.     Boolean            programIntegritySet;
  29.     
  30.     /* do integrity check before anything else; see integrity.c for details */
  31.     programIntegrityVerified=DoIntegrityCheck(&programIntegritySet);
  32.     
  33.     /* standard program initialization stuff */
  34.     MaxApplZone();    
  35.     InitGraf(&qd.thePort);
  36.     InitFonts();
  37.     FlushEvents(everyEvent, 0);
  38.     InitWindows();
  39.     InitMenus();
  40.     TEInit();
  41.     InitDialogs(0L);
  42.     InitCursor();
  43.     GetDateTime((unsigned long*)&qd.randSeed);
  44.     
  45.     InitTheDragManager();
  46.     InitTheWindowLayer();
  47.     
  48.     if (!InitTheEnvironment())            /* gestalt checks and variable initialization */
  49.         HandleError(kSystemTooOld, TRUE, TRUE);        /* less than system 7.0 */
  50.     
  51.     if (!programIntegrityVerified)    /* integrity check failed */
  52.         HandleError(kProgramIntegrityNotVerified, TRUE, FALSE);
  53.     
  54.     if (programIntegritySet)    /* integrity check freshly installed */
  55.         HandleError(kProgramIntegritySet, FALSE, TRUE);
  56.     
  57.     PrefsError(PreferencesInit());    /* get prefs (create if necessary) */
  58.     
  59.     if (!InitTheMenus())        /* get menus from .rsrc and draw menu bar */
  60.         HandleError(kProgramIntegrityNotVerified, TRUE, FALSE);
  61.         
  62.     InitThePrinting();
  63.     
  64.     InitTheProgram();
  65.     
  66.     EventLoop();                    /* where it all happens (see below) */
  67.     
  68.     ShutDownEnvironment(TRUE);        /* where it all ends (see below) */
  69.     
  70.     ExitToShell();
  71. }
  72.  
  73. void EventLoop(void)
  74. {
  75.     while (!gDone)    /* gDone set by choosing "Quit" menu item or by "quit" apple event */
  76.         HandleSingleEvent(TRUE);
  77. }
  78.  
  79. Boolean HandleSingleEvent(Boolean allowContextSwitching)
  80. {
  81.     EventRecord        theEvent;
  82.     WindowPtr        front;
  83.     
  84.     if (!gCustomCursor)
  85.         SetCursor(&qd.arrow);
  86.     HiliteMenu(0);            /* normalize menubar */
  87.     
  88.     gFrontWindowIndex=0;
  89.     front=FrontWindow();
  90.     if (front!=0L)    /* if there's a front window, see if it's one of ours */
  91.     {
  92.         SetPort(front);
  93.         gFrontWindowIndex=GetWindowIndex(front);
  94.     }
  95.         
  96.     /* get an event from the queue */
  97.     WaitNextEvent(everyEvent, &theEvent, gIsInBackground ? gBackgroundWaitTime : gForegroundWaitTime, 0L);
  98.     gTheCurrentModifiers=theEvent.modifiers;
  99.     
  100.     DispatchEvents(theEvent, allowContextSwitching);    /* handle the event we just got */
  101.     
  102.     return (theEvent.what!=nullEvent);
  103. }
  104.  
  105. short GetTheModifiers(void)
  106. {
  107.     return gTheCurrentModifiers;
  108. }
  109.  
  110. void DispatchEvents(EventRecord theEvent, Boolean allowContextSwitching)
  111. {
  112.     Point            thisPoint;
  113.     short            index;
  114.     WindowPtr        theWindow;
  115.     unsigned char    theChar;
  116.     long            dummy;
  117.     
  118.     index=gFrontWindowIndex;
  119.     
  120.     switch (theEvent.what)
  121.     {
  122.         case nullEvent:    /* ain't nuthin' happenin' */
  123.             if (gKludgeIter<3)
  124.             {
  125.                 gKludgeIter++;
  126.             }
  127.             else
  128.             {
  129.                 if (gNeedToOpenWindow)
  130.                 {
  131.                     OpenTheIndWindow(kMainWindow);
  132.                     gNeedToOpenWindow=FALSE;
  133.                 }
  134.             }
  135.             
  136.             thisPoint=theEvent.where;
  137.             GlobalToLocal(&thisPoint);
  138.             theWindow=FrontWindow();
  139.             if (WindowIsFloat(theWindow))
  140.             {
  141.                 if (IdleWindowDispatch(index, thisPoint)==kPassThrough)
  142.                 {
  143.                     if ((theWindow=GetFrontDocumentWindow())!=0L)
  144.                     {
  145.                         index=GetWindowIndex(theWindow);
  146.                         SetPort(theWindow);
  147.                         thisPoint=theEvent.where;
  148.                         GlobalToLocal(&thisPoint);
  149.                         IdleWindowDispatch(index, thisPoint);
  150.                     }
  151.                 }
  152.             }
  153.             else
  154.             {
  155.                 IdleWindowDispatch(index, thisPoint);
  156.             }
  157.             break;
  158.         case mouseDown:    /* mouse button pressed */
  159.             HandleMouseDown(theEvent, allowContextSwitching);    /* see below for mousedown handling */
  160.             break;
  161.         case mouseUp:
  162.             HandleMouseUp(theEvent);
  163.             break;
  164.         case keyDown:    /* key pressed */
  165.         case autoKey:    /* key help down */
  166.             theChar=(char)(theEvent.message & charCodeMask);
  167.             if (theEvent.modifiers & cmdKey)
  168.             {
  169.                 AdjustMenus();
  170. #if USE_MERCUTIO
  171.                 dummy=PowerMenuKey(theEvent.message, theEvent.modifiers, gBuildMenu);
  172.                 if (dummy==0L)
  173. #endif
  174.                     dummy=MenuKey(theChar);
  175.                 HandleMenu(dummy);
  176.             }
  177.             else
  178.             {
  179.                 theWindow=FrontWindow();
  180.                 if (WindowIsFloat(theWindow))
  181.                 {
  182.                     if (KeyDownDispatch(index, theChar)==kPassThrough)
  183.                     {
  184.                         if ((theWindow=GetFrontDocumentWindow())!=0L)
  185.                         {
  186.                             index=GetWindowIndex(theWindow);
  187.                             KeyDownDispatch(index, theChar);
  188.                         }
  189.                     }
  190.                 }
  191.                 else
  192.                 {
  193.                     KeyDownDispatch(index, theChar);
  194.                 }
  195.             }
  196.             ResetHiliteRgn(theWindow);
  197.             break;
  198.         case diskEvt:    /* disk insert */
  199.             if (HiWord(theEvent.message)!=noErr)    /* bad disk inserted */
  200.             {
  201.                 DILoad();    /* load disk initialization package */
  202.                 SetPt(&thisPoint, 120, 120);
  203.                 DIBadMount(thisPoint, theEvent.message);    /* give format? dialog */
  204.                 DIUnload();    /* unload 'cuz we certainly don't need it */
  205.             }
  206.             break;
  207.         case updateEvt:    /* window update */
  208.             theWindow=(WindowPtr)theEvent.message;    /* which window? */
  209.             BeginUpdate(theWindow);        /* means: "OK, we're dealing with this now" */
  210.             UpdateTheWindow(theWindow);
  211.             EndUpdate(theWindow);        /* means: "OK, we're done updating now" */
  212.             break;
  213.         case activateEvt:    /* window activate or deactivate */
  214.             if (gIgnoreNextActivateEvent)
  215.                 gIgnoreNextActivateEvent=FALSE;
  216.             else
  217.             {
  218.                 index=GetWindowIndex((WindowPtr)theEvent.message);
  219.                 if ((theEvent.modifiers & activeFlag)!=0)
  220.                     ActivateWindowDispatch(index);
  221.                 else
  222.                     DeactivateWindowDispatch(index);
  223.             }
  224.             break;
  225.         case osEvt:            /* suspend or resume program execution (switch in/out) */
  226.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  227.             {
  228.                 /* keep track of whether we're in the background or foreground */
  229.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  230.                 
  231.                 if (gIsInBackground)
  232.                     DeactivateWindowDispatch(index);
  233.                 else
  234.                     ActivateWindowDispatch(index);
  235.                 
  236.                 /* if we just came into the foreground and we have a pending error,
  237.                    now's the time to display it */
  238.                 if ((!gIsInBackground) && (gPendingErrorRec.resultCode!=allsWell))
  239.                 {
  240.                     if (gHasNotificationManager)
  241.                         NMRemove(&gPendingErrorRec.notificationRec);        /* remove notification request */
  242.                     HandleError(gPendingErrorRec.resultCode, gPendingErrorRec.isFatal,
  243.                         gPendingErrorRec.isSmall);    /* display alert, see error.c */
  244.                     gPendingErrorRec.resultCode=allsWell;        /* ...now it is */
  245.                 }
  246.                 
  247.                 if (!gIsInBackground)
  248.                     RebuildModulesList();
  249.             }
  250.             break;
  251.         case kHighLevelEvent:    /* apple event */
  252.             AEProcessAppleEvent(&theEvent);        /* see apple events.c */
  253.             break;
  254.     }
  255. }
  256.  
  257. void HandleMouseUp(EventRecord theEvent)
  258. {
  259.     WindowPtr        theWindow;
  260.     short            windowCode;
  261.     Point            theLocalPoint;
  262.     short            index;
  263.     
  264.     windowCode=FindWindow(theEvent.where, &theWindow);    /* which window? */
  265.     index=GetWindowIndex(theWindow);
  266.     
  267.     if (windowCode==inContent)
  268.     {
  269.         theLocalPoint=theEvent.where;
  270.         SetPort(theWindow);
  271.         GlobalToLocal(&theLocalPoint);
  272.         MouseUpDispatch(index, theLocalPoint);
  273.     }
  274. }
  275.  
  276. void HandleMouseDown(EventRecord theEvent, Boolean allowContextSwitching)
  277. {
  278.     WindowPtr        theWindow;
  279.     short            windowCode;
  280.     long            windSize;
  281.     GrafPtr            oldPort;
  282.     Rect            sizeRect;
  283.     short            index;
  284.     Point            theLocalPoint;
  285.     
  286.     windowCode=FindWindow(theEvent.where, &theWindow);    /* which window? */
  287.     index=GetWindowIndex(theWindow);
  288.     
  289.     switch (windowCode)
  290.     {
  291.         case inMenuBar:        /* in menu bar; let system take over */
  292.             AdjustMenus();
  293.             HandleMenu(MenuSelect(theEvent.where));
  294.             break;
  295.         case inContent:        /* in window content */
  296.             if (!DragInWindow(theWindow, &theEvent))
  297.             {
  298.                 if (!MySelectWindow(theWindow))        /* didn't need to change front windows */
  299.                 {
  300.                     theLocalPoint=theEvent.where;
  301.                     SetPort(theWindow);
  302.                     GlobalToLocal(&theLocalPoint);
  303.                     MouseDownDispatch(index, theLocalPoint);
  304.                 }
  305.             }
  306.             ResetHiliteRgn(theWindow);
  307.             break;
  308.         case inSysWindow:    /* in system window (desk accessory) */
  309.             if (allowContextSwitching)
  310.                 SystemClick(&theEvent, theWindow);    /* let the system deal with it */
  311.             break;
  312.         case inDrag:        /* in drag _region_, that is */
  313.             /* the accepted way to draw a window */
  314.             DragWindow(theWindow, theEvent.where, &((**GetGrayRgn()).rgnBBox));
  315.             /* update window bounds in window data struct */
  316.             SetWindowBounds(theWindow,
  317.                 (*(((WindowPeek)theWindow)->contRgn))->rgnBBox);
  318.             theLocalPoint.v=GetWindowBounds(theWindow).top;
  319.             theLocalPoint.h=GetWindowBounds(theWindow).left;
  320.             SetWindowTopLeft(theWindow, theLocalPoint);
  321.             MySelectWindow(theWindow);
  322.             break;
  323.         case inGoAway:        /* close box */
  324.             /* the accepted way to track a close box attempt */
  325.             if (TrackGoAway(theWindow, theEvent.where))
  326.                 DoTheCloseThing((WindowPeek)theWindow);        /* see menus.c */
  327.             break;
  328.         case inGrow:        /* grow box */
  329.             /* the accepted way to grow a window */
  330.             if (GetGrowSizeDispatch(index, &sizeRect)==kFailure)
  331.                 sizeRect=qd.screenBits.bounds;
  332.             
  333.             windSize=GrowWindow(theWindow, theEvent.where, &sizeRect);
  334.             if (windSize!=0)
  335.             {
  336.                 GetPort(&oldPort);
  337.                 SetPort(theWindow);
  338.                 EraseRect(&theWindow->portRect);
  339.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  340.                 InvalRect(&theWindow->portRect);
  341.                 SetPort(oldPort);
  342.                 
  343.                 /* update window bounds in window data struct */
  344.                 GrowWindowDispatch(index);
  345.                 SetWindowBounds(theWindow,
  346.                     (*(((WindowPeek)theWindow)->contRgn))->rgnBBox);
  347.                 theLocalPoint.v=GetWindowBounds(theWindow).top;
  348.                 theLocalPoint.h=GetWindowBounds(theWindow).left;
  349.                 SetWindowTopLeft(theWindow, theLocalPoint);
  350.                 ResetHiliteRgn(theWindow);
  351.             }
  352.             break;
  353.         case inZoomIn:        /* zoom box */
  354.         case inZoomOut:
  355.             /* the accepted way to track a zoom attempt */
  356.             if (TrackBox(theWindow, theEvent.where, windowCode))
  357.             {
  358.                 GetPort(&oldPort);
  359.                 SetPort(theWindow);
  360.                 ZoomWindow(theWindow, windowCode, FALSE);
  361.                 InvalRect(&theWindow->portRect);
  362.                 SetPort(oldPort);
  363.             }
  364.             
  365.             /* update window bounds in window data struct */
  366.             ZoomWindowDispatch(index);
  367.             SetWindowBounds(theWindow,
  368.                 (*(((WindowPeek)theWindow)->contRgn))->rgnBBox);
  369.             theLocalPoint.v=GetWindowBounds(theWindow).top;
  370.             theLocalPoint.h=GetWindowBounds(theWindow).left;
  371.             SetWindowTopLeft(theWindow, theLocalPoint);
  372.             ResetHiliteRgn(theWindow);
  373.             break;
  374.     }
  375. }
  376.  
  377. void ShutDownEnvironment(Boolean fullShutdown)
  378. {
  379.     SaveThePrefs();
  380.     if (fullShutdown)
  381.     {
  382.         ShutDownTheProgram();        /* program-specific cleanup */
  383.         ShutDownTheWindowLayer();    /* do shutdown dispatch for all windows we've used */
  384.         ShutDownTheMenus();            /* release menu resources */
  385.     }
  386. }
  387.